#!/bin/sh -ex
VERSION=`date "+%y%m%d%s"`

# Cleanup old builds
rm -f dist/*

# Copy and update header file
cat opslog/cli/header.sh > dist/ops
if [ "$1" == "dev" ];then
    sed  -i ""  "s/%version%/0/g" dist/ops 
else
    sed  -i ""  "s/%version%/$VERSION/g" dist/ops
fi
# Setup latest update metadata
sed -i "" "s/version=.*/version=$VERSION/g" opslog/appengine/static/updates/latest

# Zip package and append to distribution binary
python -mcompileall opslog/cli
#zip -r dist/ops.zip opslog/__init__.pyc opslog/__init__.py opslog/cli -x header.sh -x \*.swp
zip -r dist/ops.zip opslog/__init__.pyc opslog/cli -i \*.pyc -x header.sh -x \*.swp
cat dist/ops.zip >> dist/ops
rm dist/ops.zip
chmod +x dist/ops


cp dist/ops opslog/appengine/static/updates/
echo $VERSION
